<?php
define("TOKEN","myToken");// 后台填写的token，在微信公众平台启用
$wechatObj = new wechatAPI();
$wechatObj->isValid();

class wechatAPI
{
    public function isValid()//验证微信接口，验证函数以外的代码和微信公众号开发token设置相同
    {
        $echoStr = $_GET["echostr"];
        if ($this->checkSignature()) {
            header('content-type:text');//add，一定要加入header
            echo $echoStr;
            exit;
        }
    }
    private function checkSignature()  //官方的验证函数
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
};